import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.event.*;
import javax.swing.*;
import java.util.ListIterator;
import java.util.LinkedList;


public class Gameboard extends Panel implements MouseListener

{
    BufferedImage  image;
    BlankArea[][] spot;
    State player1CurrentState, player1LastState, player2CurrentState, player2LastState;
    boolean humanVSHuman;
    boolean	firstPlayer;
    boolean canplace;
    boolean placed;
    boolean millfound;
	 boolean removePeg;
    int previouspiecex, previouspiecey;


    public Gameboard()
    {
        JFrame frame = new JFrame("Nine Man's Morris");
        JMenuBar menuBar  = new JMenuBar();
        previouspiecex = 888;
        previouspiecey = 888;


        player1CurrentState = new State();
        player1LastState = new State();
        player2CurrentState = new State();
        player2LastState = new State();
        humanVSHuman = true;
        firstPlayer = true;
        canplace = false;
        placed = false;
		  removePeg = false;



        spot = new BlankArea[3][8];
        int boxsize = 25;
        setBounds(0,0,500,500);


        for(int i = 0; i<3; i++)
        {
            for(int j = 0; j<8; j++)
            {
                spot[i][j] = new BlankArea();
                spot[i][j].setName(i+" "+j);
                spot[i][j].setBackground(Color.lightGray );
            }
        }

        for(int j = 0; j<8; j++)
        {

            for(int i = 0; i<3; i++)
            {
                if(j == 0)
                {
                    spot[i][j].setBounds((20)+(54*i) , 20+(54*i) , boxsize, boxsize);

                }
                if(j == 1)
                {
                    spot[i][j].setBounds((200) , 20+(54*i) , boxsize, boxsize);
                }
                if(j == 2)
                {
                    spot[i][j].setBounds((378)-(54*i) , (26)+(54*i) , boxsize, boxsize);
                }
                if(j == 3)
                {
                    spot[i][j].setBounds((378)-(54*i) , 205 , boxsize, boxsize);
                }
                if(j == 4)
                {
                    spot[i][j].setBounds((378)-(54*i) , (388)-(54*i) , boxsize, boxsize);
                }
                if(j == 5)
                {
                    spot[i][j].setBounds((200) ,(388)-(54*i) , boxsize, boxsize);
                }
                if(j == 6)
                {
                    spot[i][j].setBounds((20)+(54*i) , (388)-(54*i) , boxsize, boxsize);
                }
                if(j == 7)
                {
                    spot[i][j].setBounds((20)+(54*i) ,205 , boxsize, boxsize);
                }

            }

        }

        for(int i = 0; i<3; i++)
        {
            NieghborPiece temp2 = new NieghborPiece();

            temp2.x = i;
            temp2.y = 0;
            //  System.out.println(temp2.y+ " " +temp2.x);

            player1CurrentState.gameboard[i][7].nieghborlist.add(temp2);
            ListIterator< NieghborPiece > iter = player1CurrentState.gameboard[i][7].nieghborlist.listIterator();
            while (iter.hasNext())
            {
                System.out.print(iter.next()+" ");
            }



            temp2.x = i;
            temp2.y = 7;
            player1CurrentState.gameboard[i][0].nieghborlist.add(temp2);


        }



        for(int i = 0; i<3; i++)
        {



            for(int j = 0; j<7; j++)
            {
                NieghborPiece temp = new NieghborPiece();
                temp.x = i;
                temp.y = j+1;
                int temp3 = i;
                int temp4 = j+1;
                player1CurrentState.gameboard[i][j].nieghborlist.add(temp);
            }
            for(int j = 1; j<8; j++)
            {
                NieghborPiece temp = new NieghborPiece();
                temp.x = i;
                temp.y = j-1;
                player1CurrentState.gameboard[i][j].nieghborlist.add(temp);
            }

        }

        for(int i = 0; i<2; i++)
        {

            for(int j = 1; j<8; j=j+2)
            {
                NieghborPiece temp = new NieghborPiece();
                temp.x = i+1;
                temp.y = j;
                player1CurrentState.gameboard[i][j].nieghborlist.add(temp);
            }
        }

        for(int i = 1; i<3; i++)
        {

            for(int j = 1; j<8; j=j+2)
            {
                NieghborPiece temp = new NieghborPiece();
                temp.x = i-1;
                temp.y = j;
                player1CurrentState.gameboard[i][j].nieghborlist.add(temp);

            }
        }


        for(int i = 0; i<3; i++)
        {
            for(int j = 0; j<8; j++)
            {
                spot[i][j].addMouseListener(this);
                frame.add(spot[i][j]);
            }
        }


        try
        {
            File input = new File("board.jpg");
            image = ImageIO.read(input);
        }
        catch (IOException ie)
        {
            System.out.println("Error:"+ie.getMessage());
        }

        JLabel picLabel = new JLabel(new ImageIcon( image ));

        frame.add(picLabel);


        JMenu filemenu, optionsmenu,helpmenu;
        JMenuItem savefile,loadfile, restartgame,exit,undo,about;


        filemenu = new JMenu("File");
        optionsmenu = new JMenu("options");
        helpmenu = new JMenu("help");

        menuBar.add(filemenu);

        menuBar.add(optionsmenu);
        menuBar.add(helpmenu);


        Insets insets = frame.getInsets();

        restartgame = new JMenuItem("New Game");
        savefile = new JMenuItem("Save Game");
        loadfile = new JMenuItem("load Game");
        exit = new JMenuItem("exit");
        undo = new JMenuItem("undo");
        about = new JMenuItem("about");


        filemenu.add(restartgame);
        filemenu.add(savefile);
        filemenu.add(loadfile);
        filemenu.add(exit);
        optionsmenu.add(undo);
        helpmenu.add(about);

        exit.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                System.exit(0);
            }
        });

        about.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent arg0)
            {
                drawTutorial();
            }
        });

        frame.pack();
        frame.setVisible(true);


        frame.setJMenuBar(menuBar);
        frame.setSize(438, 500);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


    }


    public void paint(Graphics g)
    {
        g.drawImage( image, 0, 0, null);
    }


    public State getplayer1CurrentState()
    {
        return player1CurrentState;
    }

    public State getplayer2CurrentState()
    {
        return player2CurrentState;
    }


    public State getPlayer1LastState()
    {
        return player1LastState;
    }


    public State getPlayer2LastState()
    {
        return player2LastState;
    }


    private void aimove()
    {

    }
	 
	 
	  private void millCheck(boolean iswhite){
	  if(boardMillCheck( iswhite)){
	  removePeg = true;
	  }
	  else{
	  	firstPlayer=!firstPlayer;
	  }
	  
	  }
	 

   private boolean  boardMillCheck(boolean iswhite)
    {
	 		int milltype = 112;
			if(iswhite){
			milltype = 3;
			}
			else
			{
			milltype = -3;
			}

        int check  = 0;
        for(int i = 0; i<3; i++)
        {
            check  = player1CurrentState.gameboard[i][0].piecetype + player1CurrentState.gameboard[i][1].piecetype + player1CurrentState.gameboard[i][2].piecetype;
            if(check == milltype)
            {
                return true;
            }
            check  = player1CurrentState.gameboard[i][2].piecetype + player1CurrentState.gameboard[i][3].piecetype + player1CurrentState.gameboard[i][4].piecetype;
            if(check == milltype)
            {
                return true;
            }
            check  = player1CurrentState.gameboard[i][4].piecetype + player1CurrentState.gameboard[i][5].piecetype + player1CurrentState.gameboard[i][6].piecetype;
            if(check == milltype)
            {
                return true;
            }
            check  = player1CurrentState.gameboard[i][6].piecetype + player1CurrentState.gameboard[i][7].piecetype + player1CurrentState.gameboard[i][0].piecetype;
            if(check == milltype)
            {
                return true;
            }
        }
        for(int i=1; i<8; i=i+2)
        {
            check  = player1CurrentState.gameboard[0][i].piecetype + player1CurrentState.gameboard[1][i].piecetype + player1CurrentState.gameboard[2][i].piecetype;
            if(check == milltype)
            {
                return true;
            }
        }
		  return false;
    }
	 
	 


    private void neighborcheck(int playerid, int xcord, int ycord)
    {
        removeBlueSquares();
        ListIterator< NieghborPiece >  itr = player1CurrentState.gameboard[xcord][ycord].nieghborlist.listIterator();


        while(itr.hasNext())
        {

            NieghborPiece temp = itr.next();
            if(player1CurrentState.gameboard[temp.x][temp.y].piecetype == 0)
            {
                System.out.println(temp.x+" " +temp.y);

                player1CurrentState.gameboard[temp.x][temp.y].piecetype = 999;
                spot[temp.x][temp.y].setBackground(Color.BLUE);

            }
        }


        System.out.println();

    }

    private void removeBlueSquares()
    {
        for(int i = 0; i<3; i++)
        {

            for(int j = 0; j<8; j++)
            {

                if(player1CurrentState.gameboard[i][j].piecetype == 999)
                {
                    player1CurrentState.gameboard[i][j].piecetype = 0;
                    spot[i][j].setBackground(Color.lightGray);
                }

            }

        }



    }


    void eventOutput(String eventDescription, MouseEvent e)
    {


        String temp  = e.getComponent().getName();
        char temp1, temp2;
        int xcord,ycord;
        temp1=temp.charAt(0);
        temp2=temp.charAt(2);
        xcord = Character.getNumericValue(temp1);
        ycord = Character.getNumericValue(temp2);

        System.out.println(eventDescription+ " "+xcord+" "+ycord  +"\n");


        if(firstPlayer)
        {
            player1CurrentState.gameboard[xcord][ycord].piecetype=1;
            ((JComponent)e.getSource()).setBackground(Color.WHITE);
        }
        else
        {
            player1CurrentState.gameboard[xcord][ycord].piecetype=-1;
            ((JComponent)e.getSource()).setBackground(Color.BLACK);
        }

    }

    public void mousePressed(MouseEvent e)
    {

        String temp  = e.getComponent().getName();
        char temp1, temp2;
        int xcord,ycord; //stands for x cordinate and y cordinate
        temp1=temp.charAt(0);
        temp2=temp.charAt(2);
        xcord = Character.getNumericValue(temp1);
        ycord = Character.getNumericValue(temp2);

        if(firstPlayer)
        {

            if(player1CurrentState.whiteinhand>0 && player1CurrentState.gameboard[xcord][ycord].piecetype==0)
            {
                player1CurrentState.whiteinhand--;

                ((JComponent)e.getSource()).setBackground(Color.WHITE);
                player1CurrentState.gameboard[xcord][ycord].piecetype=1;
					 millCheck(true);

            }
            else if( player1CurrentState.gameboard[xcord][ycord].piecetype==1)
            {
                previouspiecex =xcord;
                previouspiecey = ycord;
                neighborcheck(1,xcord,ycord);


            }
            else if(player1CurrentState.gameboard[xcord][ycord].piecetype == 999)
            {
                ((JComponent)e.getSource()).setBackground(Color.WHITE);
                player1CurrentState.gameboard[xcord][ycord].piecetype = 1;
                spot[previouspiecex][previouspiecey].setBackground(Color.lightGray);
                player1CurrentState.gameboard[previouspiecex][previouspiecey].piecetype=0;
                removeBlueSquares();
					 millCheck(true);

            }
				else if( player1CurrentState.gameboard[xcord][ycord].piecetype==-1 && removePeg == true)
            {
                firstPlayer=!firstPlayer;
					 removePeg=false;
                ((JComponent)e.getSource()).setBackground(Color.lightGray);
					 player1CurrentState.gameboard[xcord][ycord].piecetype = 0;


					 
            }


        }
        else
        {


            if(player1CurrentState.blackinhand>0 && player1CurrentState.gameboard[xcord][ycord].piecetype==0)
            {
                player1CurrentState.blackinhand--;

                ((JComponent)e.getSource()).setBackground(Color.BLACK);
                player1CurrentState.gameboard[xcord][ycord].piecetype=-1;
					 millCheck(false);


            }
            else if( player1CurrentState.gameboard[xcord][ycord].piecetype==-1)
            {
                previouspiecex =xcord;
                previouspiecey = ycord;
                neighborcheck(-1,xcord,ycord);


            }
            else if(player1CurrentState.gameboard[xcord][ycord].piecetype == 999)
            {
                ((JComponent)e.getSource()).setBackground(Color.BLACK);
                player1CurrentState.gameboard[xcord][ycord].piecetype = -1;
                spot[previouspiecex][previouspiecey].setBackground(Color.lightGray);
                player1CurrentState.gameboard[previouspiecex][previouspiecey].piecetype=0;
                removeBlueSquares();

                firstPlayer=!firstPlayer;
					  millCheck(false);


            }
				else if( player1CurrentState.gameboard[xcord][ycord].piecetype==-1 && removePeg == true)
            {
                firstPlayer=!firstPlayer;
					 removePeg=false;
                ((JComponent)e.getSource()).setBackground(Color.lightGray);
					 player1CurrentState.gameboard[xcord][ycord].piecetype = 0;


					 
            }




        }


    }

    public void mouseReleased(MouseEvent e)
    {

    }

    public void mouseEntered(MouseEvent e)
    {
    }

    public void mouseExited(MouseEvent e)
    {
    }

    public void mouseClicked(MouseEvent e)
    {


    }

    public void drawTutorial()
    {

    }



    static public void main(String args[]) throws
    Exception
    {

        State currentstate = new State();
        Gameboard board = new Gameboard();
        GameData test = new GameData();
        //	board.firstPlayerLoop();


    }
}
